package com.example.myapplication.frament;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
import com.example.myapplication.adapter.NationAdater;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class FragmentNation extends Fragment {
    Button btn;
    WebView web;
    View view;
    EditText et;
    TabLayout tb;
    ViewPager vp;
    FragmentManager fm;
    List<Fragment> list=new ArrayList<>();
    List<String> list1=new ArrayList<>();

    @Nullable
    @Override


    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getContext() , R.layout.fragment_nation , null);
        //点击搜索按钮跳转WebView
        tiaozhuang();
        //搜索框
        Search();
        //导航条
        daohantiao();

        return view;
    }

    private void tiaozhuang() {
        btn=view.findViewById(R.id.btn);
        web=view.findViewById(R.id.web);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                web.loadUrl("http://minzu.folkw.com/MinZu.Asp?Name=HanZu");
                list.clear();
                list1.clear();
                daohantiao();

            }
        });
    }

    //          搜索框
    private void Search() {
        et=view.findViewById(R.id.et);
        et.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //手机上的回车进行操作
                if (keyCode==KeyEvent.ACTION_DOWN||keyCode==KeyEvent.KEYCODE_ENTER&&event.getAction()==KeyEvent.ACTION_DOWN){
                    System.out.println("kaixing");

                    list.clear();
                    list1.clear();
                    daohantiao();
                }
                return false;
            }
        });
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId== EditorInfo.IME_ACTION_SEARCH){
                    System.out.println("啦啦啦德玛西亚之力");
                    list.clear();
                    list1.clear();
                    daohantiao();
                }
                return false;
            }
        });
    }

    //      导航条
    private void daohantiao() {
        tb=view.findViewById(R.id.tb);
        vp=view.findViewById(R.id.vp);

        list1.add("概述");
        list1.add("服饰");
        list1.add("习俗");
        list1.add("建筑");

        list.add(new JianZhuFragment());
        list.add(new FushiFragment());
        list.add(new XishuFragment());
        list.add(new JianZhuFragment());
        fm=getChildFragmentManager();
        NationAdater nationAdater=new NationAdater(fm,list);
        vp.setAdapter(nationAdater);
        tb.setupWithViewPager(vp);
        tb.getTabAt(0).setText(list1.get(0));
        tb.getTabAt(1).setText(list1.get(1));
        tb.getTabAt(2).setText(list1.get(2));
        tb.getTabAt(3).setText(list1.get(3));
    }
    //退出时清除数据
    @Override
    public void onDestroyView() {
        list.clear();
        list1.clear();
        super.onDestroyView();
    }
}
